package com.android.antelopeapp.view.BeautyPic;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.antelopeapp.R;
import com.android.antelopeapp.network.NetworkManager;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.lyhw.model.BeautyPic;
import com.lyhw.model.ImageTag;

/**
 * Created by phk on 15-4-4.
 * 此控件用于展示美图
 */
public class BPicView extends LinearLayout {
    private TextView authorNickNameText;
    private TagableImage tagableImage;
    private ListView commentListView;
    private TextView desText;
    private TextView praiseText;
    private TextView commentBtn;
    private FrameLayout tagContainer;

    public BPicView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_bpic_show_item, this,true);

        authorNickNameText = (TextView)this.findViewById(R.id.author_text);
        tagableImage = (TagableImage)this.findViewById(R.id.bpic_tagedimg);
        desText = (TextView) this.findViewById(R.id.bpic_des);
        commentListView =(ListView) this.findViewById(R.id.bpic_attach_list);
        praiseText = (TextView) this.findViewById(R.id.common_praise_cnt);
        commentBtn = (TextView) this.findViewById(R.id.btn_comment);
        tagContainer = (FrameLayout) this.findViewById(R.id.show_tag_container);
    }

    /**
     * 设置用户名
     * */
    public void setAuthorNickNameText(String nickName){
        authorNickNameText.setText(nickName);
    }

    /**
     * 绑定数据，更新viewholder
     *
     * @param bpic*/
    public void bindData(final BeautyPic bpic){
        //设置用户名
        authorNickNameText.setText(bpic.getAuthor());
        //标签设置为不可见
        tagContainer.setVisibility(INVISIBLE);
        //设置图片，当图片导入成功后，标签设置为可见
        if(bpic.getPic()!=null) {
            //发送网络请求获取
            ImageLoader imageLoader = NetworkManager.getInstance(this.getContext()).getImageLoader();
            imageLoader.get(
                    NetworkManager.getResourceAbsPath(bpic.getPic()),
                    new ImageLoader.ImageListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            tagableImage.getmImage().setImageResource(R.drawable.error_img);
                        }

                        @Override
                        public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                            if (response.getBitmap() != null) {
                                tagableImage.getmImage().setImageBitmap(response.getBitmap());
                                tagContainer.setVisibility(VISIBLE);
                            } else if (R.drawable.default_img != 0) {
                                tagableImage.getmImage().setImageResource(R.drawable.default_img);
                            }
                        }
                    });
        }
        //清空标签
        tagableImage.clearTagQueue();
        tagContainer.removeAllViews();
        //设置标签
        tagableImage.addImgTags(ImageTag.labels2tags(bpic.getLabelList()));
        //设置内容
        desText.setText(bpic.getContent());
        //点赞数
        praiseText.setText(""+bpic.getPraise());
        praiseText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //todo 点赞
                Toast.makeText(BPicView.this.getContext(), "点赞", Toast.LENGTH_SHORT);
                Log.d("BPicView","点赞");
            }
        });
        //评论按钮
        commentBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //todo 评论
                Toast.makeText(BPicView.this.getContext(),"评论",Toast.LENGTH_SHORT);
                Log.d("BPicView","评论");
            }
        });
    }

    public TagableImage getTagableImage() {
        return tagableImage;
    }
}
